﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace WPFMitsuControlsv2
{
    public static class WPFHelper
    {
        /// <summary>
        /// Seach for a visual Child of type T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }

        /// <summary>
        ///     Walks up the templated parent tree looking for a parent type.
        /// </summary>
        public static T FindParent<T>(FrameworkElement element) where T : FrameworkElement
        {
            FrameworkElement parent = element.TemplatedParent as FrameworkElement;

            while (parent != null)
            {
                T correctlyTyped = parent as T;
                if (correctlyTyped != null)
                {
                    return correctlyTyped;
                }

                parent = parent.TemplatedParent as FrameworkElement;
            }

            return null;
        }

        public static T FindVisualParent<T>(UIElement element) where T : UIElement
        {
            UIElement parent = element;
            while (parent != null)
            {
                T correctlyTyped = parent as T;
                if (correctlyTyped != null)
                {
                    return correctlyTyped;
                }

                parent = VisualTreeHelper.GetParent(parent) as UIElement;
            }
            return null;
        } 


        //#region "GetCell"

        //public static DataGridCell GetCell(DataGrid dataGrid, int row, int column)
        //{
        //    DataGridRow rowContainer = GetRow(dataGrid, row);
        //    if (rowContainer != null)
        //    {
        //        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

        //        // try to get the cell but it may possibly be virtualized 
        //        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        //        if (cell == null)
        //        {
        //            // now try to bring into view and retreive the cell 
        //            dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);

        //            cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        //        }

        //        return cell;
        //    }

        //    return null;
        //}

        //public static DataGridCell GetCell(DataGrid dataGrid, DataGridRow row, int column)
        //{
        //    if (row != null)
        //    {
        //        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

        //        // try to get the cell but it may possibly be virtualized 
        //        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        //        if (cell == null)
        //        {
        //            // now try to bring into view and retreive the cell 
        //            dataGrid.ScrollIntoView(row, dataGrid.Columns[column]);

        //            cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        //        }

        //        return cell;
        //    }

        //    return null;
        //}

        //public static DataGridCell GetCell(DataGrid dataGrid, DataGridRow row, DataGridCellsPresenter presenter, int column)
        //{

        //    // try to get the cell but it may possibly be virtualized 
        //    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        //    if (cell == null)
        //    {
        //        // now try to bring into view and retreive the cell 
        //        dataGrid.ScrollIntoView(row, dataGrid.Columns[column]);

        //        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        //    }

        //    return cell;

        //}

        //#endregion

        //#region "GetRow"

        ///// <summary> 
        ///// Gets the DataGridRow based on the given index 
        ///// </summary> 
        ///// <param name="index">the index of the container to get</param> 
        //public static DataGridRow GetRow(DataGrid dataGrid, int index)
        //{
        //    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
        //    if (row == null)
        //    {
        //        // may be virtualized, bring into view and try again 
        //        dataGrid.ScrollIntoView(dataGrid.Items[index]);
        //        dataGrid.UpdateLayout();

        //        row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
        //    }

        //    return row;
        //}

        //#endregion

        //#region "GetRowHeader"

        ///// <summary> 
        ///// Gets the DataGridRowHeader based on the row index. 
        ///// </summary> 
        ///// <param name="index"></param> 
        ///// <returns></returns> 
        //public static DataGridRowHeader GetRowHeader(DataGrid dataGrid, int index)
        //{
        //    return GetRowHeader(GetRow(dataGrid, index));
        //}

        ///// <summary> 
        ///// Returns the DataGridRowHeader based on the given row. 
        ///// </summary> 
        ///// <param name="row">Uses reflection to access and return RowHeader</param> 
        //public static DataGridRowHeader GetRowHeader(DataGridRow row)
        //{
        //    if (row != null)
        //    {
        //        return GetVisualChild<DataGridRowHeader>(row);
        //    }
        //    return null;
        //}

        //#endregion

        //#region "GetColumnHeader"

        //public static DataGridColumnHeader GetColumnHeader(DataGrid dataGrid, int index)
        //{
        //    DataGridColumnHeadersPresenter presenter = GetVisualChild<DataGridColumnHeadersPresenter>(dataGrid);

        //    if (presenter != null)
        //    {
        //        return (DataGridColumnHeader)presenter.ItemContainerGenerator.ContainerFromIndex(index);
        //    }

        //    return null;
        //}

        //#endregion

        ///// <summary>
        ///// Ajoute ou supprime le filtre "method" à la CollectionViewSource en fonction de la selection
        ///// </summary>
        ///// <param name="filtreCB"></param>
        ///// <param name="cvs"></param>
        ///// <param name="method"></param>
        //public static void GererFiltreCombobox(ComboBox filtreCB, CollectionViewSource cvs, FilterEventHandler method)
        //{
        //    GererFiltreCombobox(filtreCB, cvs, method, 0);
        //}

        ///// <summary>
        ///// Ajoute ou supprime le filtre "method" à la CollectionViewSource en fonction de la selection
        ///// </summary>
        ///// <param name="filtreCB"></param>
        ///// <param name="cvs"></param>
        ///// <param name="method"></param>
        ///// <param name="sansFiltreIndex"></param>
        //public static void GererFiltreCombobox(bool ajouter, CollectionViewSource cvs, FilterEventHandler method, int sansFiltreIndex)
        //{

        //    if (ajouter)
        //    {

        //        // supprimer le filtre
        //        cvs.Filter -= method;
        //    }
        //    else
        //    {
        //        cvs.Filter += method;
        //    }
        //    //else if (e.RemovedItems(0).GetType().AssemblyQualifiedName == typeDonnee.AssemblyQualifiedName & e.AddedItems(0) is ComboBoxItem)
        //    //else if (e.RemovedItems(0) is ComboBoxItem & e.AddedItems(0).GetType().AssemblyQualifiedName == typeDonnee.AssemblyQualifiedName)

        //}

    }
}
